#include "app/oogameapp.h"
#include "game/oogameworld.h"
#include "graphics/oorender.h"
#include "graphics/oodisplaywindow.h"
#include "graphics/ooimagecodec.h"
#include "input/ooinputmanager.h"

//------------------------------------------------------------------------
ImplementSingleton(ooGameApp)

//------------------------------------------------------------------------
ooGameApp::ooGameApp()
:m_displayWindow(DBBNULL)
,m_render(DBBNULL)
,m_imageCodec(DBBNULL)
,m_inputManager(DBBNULL)
,m_gameWorld(DBBNULL)
,m_logger(DBBNULL)
,m_quit(false)
{
	ConstructSingleton
	_init();
}

//------------------------------------------------------------------------
ooGameApp::~ooGameApp()
{
	_cleanup();
	DestructSingleton
}

//------------------------------------------------------------------------
void
ooGameApp::setAppName(const String& name)
{
	m_displayWindow->setName(name);
}

//------------------------------------------------------------------------
void
ooGameApp::setWindowSize(int width, int height)
{
	m_displayWindow->setSize(width,height);
}

//------------------------------------------------------------------------
void
ooGameApp::setWindowed(bool b)
{

}

//------------------------------------------------------------------------
void 
ooGameApp::run()
{
	m_displayWindow->openWindow();
	m_render->focusWindow(m_displayWindow);
	while(!m_quit)
	{
		float elapsed = m_timer.tickElapsed();
		m_displayWindow->tick(elapsed);
		m_inputManager->tick(elapsed);
		m_render->tick(elapsed);
		m_gameWorld->tick(elapsed);
	}
	m_displayWindow->closeWindow();
}

//------------------------------------------------------------------------
void 
ooGameApp::postQuitMessage()
{
	m_quit = true;
}

//------------------------------------------------------------------------
void 
ooGameApp::_init()
{
	m_logger		= new ooConsoleLogger();
	m_displayWindow = new ooDisplayWindow();
	m_imageCodec	= new ooImageCodec();
	m_render		= new ooRender();
	m_inputManager	= new ooInputManager();
	m_gameWorld		= new ooGameWorld();
}

//------------------------------------------------------------------------
void
ooGameApp::_cleanup()
{
	delete m_displayWindow;
	delete m_render;
	delete m_imageCodec;
	delete m_inputManager;
	delete m_gameWorld;
	delete m_logger;
}